import { getCart } from '@/api/shopcart'
import { createStore } from 'vuex'

export default createStore({
  state: {
    user: {
      isLogin: !!window.localStorage.getItem('token')
    },
    cartCount: 0
  },
  mutations: {
    setUser (state, newUser) {
      state.user = newUser
    },
    setCartCount (state, newCartCount) {
      state.cartCount = newCartCount
    }
  },
  actions: {
    updateCartCount (state) {
      getCart().then(res => {
        if (res.status === 200) {
          state.commit('setCartCount', res.data.data.length)
        }
      })
    }
  },
  modules: {
  }
})
